package com.shizhanzhe.gxb.home.adapter;

import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.shizhanzhe.gxb.R;
import com.shizhanzhe.gxb.base.UserBean;
import com.shizhanzhe.gxb.home.bean.PDDSearch;
import com.shizhanzhe.gxb.utils.Util;

import java.util.List;

/**
 * Created by zz9527 on 2018/12/19.
 */
public class SearchAdapter extends BaseAdapter {
    private final LayoutInflater inflater;
    private Context mContext;
    private List<PDDSearch.DataBean> list;

    public SearchAdapter(List<PDDSearch.DataBean> list, Context context) {
        this.list = list;
        this.mContext = context;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.adapter_pdditem, null);
            holder = new ViewHolder();
            holder.img = convertView.findViewById(R.id.iv_logo);
            holder.title = convertView.findViewById(R.id.tv_title);
            holder.qhprice = convertView.findViewById(R.id.tv_quan_hou_price);
            holder.nowprice = convertView.findViewById(R.id.tv_price_on_line);
            holder.yj = convertView.findViewById(R.id.tv_yong_price);
            holder.sell = convertView.findViewById(R.id.tv_sell_count);
            holder.level = convertView.findViewById(R.id.tv_up_price);
            holder.quan = convertView.findViewById(R.id.quan);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        PDDSearch.DataBean bean = list.get(i);
        Glide.with(mContext).load(bean.getGoods_thumbnail_url()).apply(new RequestOptions().error(R.mipmap.load_img_error)).into(holder.img);
        holder.title.setText(bean.getGoods_name());
        double price = bean.getMin_group_price() / 100;
        double qhprice = ((double) bean.getMin_group_price() - (double) bean.getCoupon_discount()) / 100;
        String yj = Util.Sub(qhprice * ((double) bean.getPromotion_rate() / 1000) *  UserBean.vipdivide + "");
        String yj2= Util.Sub(qhprice*((double)bean.getPromotion_rate()/1000)* UserBean.divide+"");
        holder.qhprice.setText("¥"+qhprice);
        holder.nowprice.setText("¥"+price);
        holder.nowprice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG );
        holder.yj.setText("预计赚¥"+yj2);
        holder.level.setText("升级赚¥"+yj);
        holder.sell.setText("月销量:"+bean.getSold_quantity());
        holder.quan.setText(bean.getCoupon_discount()/100+"元券");
        if (bean.getCoupon_discount() / 100==0){
            holder.quan.setVisibility(View.GONE);
            holder.nowprice.setVisibility(View.GONE);
        }
        return convertView;
    }

    private static class ViewHolder {
        ImageView img;
        TextView title;
        TextView qhprice;
        TextView nowprice;
        TextView yj;
        TextView sell;
        TextView level;
        TextView quan;
    }
}
